﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace DoAn
{
    public partial class frmHopdongvayvon : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;
    
        public frmHopdongvayvon()
        {
            String conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            conn = new SqlConnection(conStr);
            conn.ConnectionString = conStr;
            InitializeComponent();
        }

        private void frmHopdongvayvon_Load(object sender, EventArgs e)
        {
            //conn.Open();
            cmd = new SqlCommand("SELECT * FROM hopdongvayvon", conn);
            da = new SqlDataAdapter(cmd);
            ds = new DataSet();
            da.Fill(ds, "hopdongvayvon");
            cmb = new SqlCommandBuilder(da);
            dataGridView1.DataSource = ds.Tables[0];
            //cbhtv.DataSource = ds.Tables[0];
            //cbhtv.ValueMember = ds.Tables[0].Columns[0].ToString();
           // cbhtv.DisplayMember = ds.Tables[0].Columns[0].ToString();
           // da.Dispose();
            
            conn.Close();
        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbhdvv.Text == "" || tbtkh.Text == "" || tbtstc.Text == "" || tbstv.Text == "" || tbmkh.Text == "" || dtngayvay.Text == "" || dtngaykt.Text == "" || tbtlv.Text == "" || cbhtv.Text == "" )
                {
                    MessageBox.Show("Bạn chưa điền đầy đủ thông tin", "Có lỗi xảy ra!");
                    tbhdvv.Focus();
                    return;
                }
                else
                {
                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM hopdongvayvon", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "hopdongvayvon");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                    DataTable dt = ds.Tables[0];
                    DataRow newRow = dt.NewRow();
                    newRow["mahd"] = tbhdvv.Text;
                    newRow["makh"] = tbmkh.Text;
                    newRow["tenkhachhang"] = tbtkh.Text;
                    newRow["hdvvtongtaisanthechap"] = tbtstc.Text;
                    newRow["hdvvsotienvay"] = tbstv.Text;
                    newRow["htvten"] = cbhtv.Text;
                    
                    newRow["tllmuclai"] = tbtlv.Text;
                    newRow["hdvvngayvay"] = dtngayvay.Text;
                    newRow["hdvvngayketthuc"] = dtngaykt.Text;
                    dt.Rows.Add(newRow);
                    da.Update(ds, "hopdongvayvon");
                    MessageBox.Show("Thêm thành công");
                }

            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
                tbhdvv.Focus();

            } 
        }

        private void tbhdvv_TextChanged(object sender, EventArgs e)
        {

        }

        private void tbmkh_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            ds.Tables[0].Rows[i].Delete();
            da.Update(ds, "hopdongvayvon");
            MessageBox.Show("Xóa thành công");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            frmMenu objfrmMenu = new frmMenu();
            this.Dispose();
            objfrmMenu.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiem.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiem.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM hopdongvayvon where mahd  = '" + tbtimkiem.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "hopdongvayvon");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbtimkiem1.Text == "")
                {
                    MessageBox.Show("Bạn chưa điền tên", "Có lỗi xảy ra!");
                    tbtimkiem1.Focus();
                    return;
                }
                else
                {

                    conn.Open();
                    cmd = new SqlCommand("SELECT * FROM hopdongvayvon where makh  = '" + tbtimkiem1.Text + "'", conn);
                    da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds, "hopdongvayvon");
                    cmb = new SqlCommandBuilder(da);
                    dataGridView1.DataSource = ds.Tables[0];
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tbhdvv.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tbmkh.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tbtkh.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            cbhtv.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString();
            tbtstc.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            tbtlv.Text = dataGridView1.CurrentRow.Cells[6].Value.ToString();
            tbstv.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
            dtngayvay.Text = dataGridView1.CurrentRow.Cells[7].Value.ToString();
            dtngaykt.Text = dataGridView1.CurrentRow.Cells[8].Value.ToString();
        }
        

        private void btSua(object sender, EventArgs e)
        {
             try 
             {if (tbhdvv.Text == "")
                {
                    MessageBox.Show("Bắt đầu sửa", "Có lỗi xảy ra!");
                    tbhdvv.Focus();
                    return;
                }
                  else
                {
            String mahopdong = tbhdvv.Text;
            String makhach = tbmkh.Text;
            String tenkh = tbtkh.Text;
            String tongtaisan = tbtstc.Text;
            String sotienvay = tbstv.Text;
            String hinhthucvay = cbhtv.Text;
            String tylevay = tbtlv.Text;
            String ngayvay = dtngayvay.Value.ToString();
            String ngaykt = dtngaykt.Value.ToString();

            //int mahd = Int32.Parse(mahopdong);
            int mahd = Int32.Parse(mahopdong);
            int mak = Int32.Parse(makhach);
            int tongts = Int32.Parse(tongtaisan);
            int sotien = Int32.Parse(sotienvay);
            conn.Open();
            //String sql = 
            cmd = new SqlCommand("Update hopdongvayvon set makh='" + mak + "', tenkhachhang = '" + tenkh + "',hdvvtongtaisanthechap= " + tongts + ",hdvvsotienvay = " + sotien + ",htvten = '" + hinhthucvay + "',tllmuclai = '" + tylevay + "', hdvvngayvay = '" + ngayvay + "',hdvvngayketthuc = '" + ngaykt + "' where mahd=" + mahd + "", conn);
            da = new SqlDataAdapter(cmd);
            int i = cmd.ExecuteNonQuery();
            ds = new DataSet();
            da.Fill(ds, "hopdongvayvon");
            cmb = new SqlCommandBuilder(da);
            //dataGridView1.DataSource = ds.Tables[0];
            // this.frmHopdongvayvon_Load();
            //this.LoadForm();
            this.frmHopdongvayvon_Load(sender, e);
            MessageBox.Show("Sửa thành công");
                 conn.Close();
                }
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
                 MessageBox.Show(" thành công");
             }
        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        
    }
}
